--
-- Author: yawei.zhu
-- Date: 2018-07-03 10:58:44
--
-- OtherPlayerView
--

local NewPlayerPanel = require("app.newBag.views.NewPlayerPanel")
local OtherPlayerRender = import(".renders.OtherPlayerRender")
local NewListAdapterTool = require("app.widget.NewListAdapterTool")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.OtherPlayerUi

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)

	-- 左边
	self._playerPanel = NewPlayerPanel.create(self._ui.player)
	self._playerPanel:setAttrAndStrengthShown( true )
	self._playerPanel:setIsOtherPlayer(true)
	self._playerPanel:setClickHandler(function(op, render) -- 人物身上的操作
			local renderData = render:getData()
			if render:isSelected() and renderData then
				if "PlayerPanel_showEquipTip" == op then -- 选中装备
					executeFunc(self._onOperate, "PlayerPanel_showEquipTip", render)
				elseif "PlayerPanel_selectedOtherEquipWhenStrengthShown" == op then -- 在装备强化UI显示时选中其他装备
					assert( self._equipStrengthenPanel, " ------>>>> 在装备强化UI显示时选中其他装备,但是panel却不存在了 " )

					local curData = self._equipStrengthenPanel:getData()
					if curData.uid == renderData.uid then
						return
					else
						self._equipStrengthenPanel:setData( renderData )
					end
				elseif "PlayerPanel_selectedClothes" == op then -- 选中时装、需要在衣橱中定位到
					executeFunc(self._onOperate, "PlayerPanel_selectedClothes", render)
				end
			end
		end)

	-- 右边
	local rightNode = self._ui.count:getChildByName("Panel")
	local tlt = rightNode:findChild("Panel_tlt/tlt")
	tlt:setString( L("lua_code_text_145") )

	self._scrollView = rightNode:getChildByName("ScrollView")

	-- list 		 	列表容器，不能为空
	-- subPanelClass 	子renderClass，不能为空
	-- initEndCb 		初始化完成回调,可以为空
	self._rareList = NewListAdapterTool.new( {list = self._scrollView, subPanelClass = OtherPlayerRender} )
end

function ClassRef:updateView(userInfo)
	self._playerPanel:setData(userInfo)
	self._playerPanel:refreshProp(userInfo.showAttrList)

	local infos = userInfo.sinfos

	local function findUnit(id)
		for _, data in ipairs(infos) do
			if data.id == id then
				return data
			end
		end
		return nil
	end 

	-- 更新统计数据
	local statisticInfos = GD:getStatisticsInfo()
	for _, data in ipairs(statisticInfos) do
		data.value = 0
		local funcId = tonumber(data.func)
		if funcId == Enums.statisticId.ACHIEVE_POINT then
			data.value = userInfo.achvPoint
		elseif funcId == Enums.statisticId.MOUNT_NUM then
			data.value = userInfo.mountNum
		elseif funcId == Enums.statisticId.PARTNER_NUM then
			data.value = userInfo.partnerNum
		elseif funcId == Enums.statisticId.GUILD_NAME then
			data.value = userInfo.baseInfo.guildName
		else
			local info = findUnit(funcId)
			if info then
				data.value = info.value
			end
		end
	end
	self._rareList:refreshList(statisticInfos)
end

return ClassRef
